package com.rc.saas.tenant.controller.ugs;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.component.UploadImg;
import com.rc.saas.tenant.config.UpYunConfig;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelGain;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelGainExample;
import com.rc.saas.tenant.service.ugs.TenantUgsLevelGainService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.vo.ugs.TenantUgsLevelGainDto;
import com.rc.saas.tenant.vo.ugs.TenantUgsLevelGainVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2019/11/23
 */
@Controller
@RequestMapping("/tenantUgsLevelGain")
public class TenantUgsLevelGainController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(TenantUgsLevelGainController.class);

    @Autowired
    private TenantUgsLevelGainService tenantUgsLevelGainService;

    @Autowired
    private UpYunConfig upYunConfig;

    @Autowired
    private UploadImg uploadImg;

    /**
     * 跳转等级权益设置页面
     *
     * @return
     */
    @RequestMapping(value = "/ugsLevelType", method = RequestMethod.GET)
    public String ugsLevelType() {
        return "/templates/ugs/tenantUgsLevelGain";
    }

    /**
     * 跳转超级会员权益设置页面
     *
     * @return
     */
    @RequestMapping(value = "/ugsSuperLevelType", method = RequestMethod.GET)
    public String tenantUgsLevelGain() {
        return "/templates/ugs/tenantUgsLevelGainSuper";
    }

    /**
     * 返回等级权益设置列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantUgsLevelGain", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantUgsLevelGain(TenantUgsLevelGainDto tenantUgsLevelGainDTO) {
        tenantUgsLevelGainDTO.setLvType(tenantUgsLevelGainDTO.getLvType());
        PageInfo<TenantUgsLevelGainVo> pageInfo = tenantUgsLevelGainService.listPageTenantUgsLevelGain(tenantUgsLevelGainDTO);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转编辑等级权益设置列表页面
     *
     * @return
     */
    @RequestMapping(value = "/editTenantUgsLevelGain/{levelGainTypeId}", method = RequestMethod.GET)
    public String editTenantUgsLevelGain(@PathVariable("levelGainTypeId") Long levelGainTypeId, Model model) {
        TenantUgsLevelGain tenantUgsLevelGain = tenantUgsLevelGainService.selectByPrimaryKey(levelGainTypeId);
        model.addAttribute("uploadPath", upYunConfig.getDownloadRoot());
        model.addAttribute("tenantUgsLevelGain", tenantUgsLevelGain);
        return "/templates/ugs/editTenantUgsLevelGain";
    }

    /**
     * 保存/编辑等级权益
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantUgsLevelGain", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateBanner(TenantUgsLevelGain tenantUgsLevelGain, @RequestParam(value = "file", required = false) MultipartFile file) {
        if (!BeanUtil.isValid(tenantUgsLevelGain.getLevelGainTypeName(), tenantUgsLevelGain.getSort())) {
            return TResponse.FAIL("请输入正确的参数");
        }

        TenantUser user = ShiroUtils.getSessionUser();

        TenantUgsLevelGainExample example = new TenantUgsLevelGainExample();
        example.createCriteria().andTenantCodeEqualTo(user.getTenantCode()).andLvTypeEqualTo(tenantUgsLevelGain.getLvType())
                .andSortEqualTo(tenantUgsLevelGain.getSort());
        List<TenantUgsLevelGain> tenantUgsLevelGains = tenantUgsLevelGainService.selectByExample(example);
        if (!CollectionUtils.isEmpty(tenantUgsLevelGains)){
            if (tenantUgsLevelGain.getLevelGainTypeId()==null){
                return TResponse.FAIL("排序存在重复");
            }else {
                if (!tenantUgsLevelGains.get(0).getLevelGainTypeId().equals(tenantUgsLevelGain.getLevelGainTypeId())){
                    return TResponse.FAIL("排序存在重复");
                }
            }
        }

        try {
//            if (file.getSize() == 0 && tenantUgsLevelGain.getLevelGainTypeId() == null) {
//                return TResponse.FAIL("请上传图片");
//            }
            if (file.getSize() != 0) {
                TResponse tResponse = uploadImg.uploadImg(user.getTenantCode(), file);
                if (tResponse.getStatus() == 200) {
                    tenantUgsLevelGain.setGainTypeImg((String) tResponse.getData());
                } else {
                    return tResponse;
                }
            }
            tenantUgsLevelGain.setLastUpdatedBy(user.getLoginName());
            tenantUgsLevelGain.setLastUpdatedTime(new Date());
            if (tenantUgsLevelGain.getLevelGainTypeId() != null) {
                tenantUgsLevelGainService.updateByPrimaryKeySelective(tenantUgsLevelGain);
            } else {
                tenantUgsLevelGain.setTenantCode(user.getTenantCode());
                tenantUgsLevelGainService.insertSelective(tenantUgsLevelGain);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存等级权益失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 更改等级权益状态
     *
     * @return
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateStatus(Long levelGainTypeId) {
        try {
            TResponse tResponse = tenantUgsLevelGainService.updateStatus(levelGainTypeId);
            return tResponse;
        } catch (Exception e) {
            logger.error("更改等级权益状态失败", e);
            return TResponse.FAIL("操作失败");
        }
    }
}
